#ifndef _REGION_MANANGER_H_
#define _REGION_MANANGER_H_

#include "Common.h"
#include "Resource.h"
#include "ResourceManager.h"
#include "Singleton.h"
#include "String.h"
#include "Threading/ThreadStarter.h"
#include "Threading/QueueThreadPool.h"
#include "NGLog.h"
#include "Rectangle.h"


class  RegionManager: public ThreadRunableBase,public ResourceManager,public Singleton<RegionManager>
{
public:
	RegionManager();
	~RegionManager();

	ResourcePtr  create(const uint32 & serial,const NameValuePairList* params = 0);
	ResourceCreateOrRetrieveResult  \
		createOrRetrieve(const uint32 & serial,const NameValuePairList* params = 0);
	ResourcePtr  load(const uint32 & serial);
	ResourcePtr  load(const uint16 & region_x,const uint16 & region_y);
	ResourcePtr  load(const float & x,const float & y);
	ResourcePtr  load(const Vector3 & worldPos);
	
	//得到区域的坐标 RegionX RegionY
	Vector3		getRegionPosition(const Vector3 & worldPos);
	Vector3		getRegionPosition(const float & x,const float & y);
	
	uint16		getRegionX(const float & worldX);
	uint16		getRegionY(const float & worldY);

	//user manager start
	void	Startup(const uint32 & server_id,const uint32 & gid,const Rectangle & world_rect, const uint32 & block_size,	\
				const uint32 & thread_min,const uint32 & thread_max = 0);
	void 	Shutdown(void);

	//virual from  ThreadRunableBase
        bool  run(void *p);

	bool	StartRegionProcess(void);

	const uint32 & getServerID(void)	{	return m_server_id;	}
	const uint32 & getGroupID(void)		{	return m_default_gid;	}
	size_t 	getRegionList(std::list<ResourcePtr> & cList);

	const uint32 getCurrentUpdateTime(void)	{	return m_currentUpdateTime;	}

	void	setAutoRegionCheck(const bool & set)	{	m_autoRegionCheck = set;	}


	//得到世界范围
	const Rectangle & getWorldRectangle(void)	{	return m_world_rect;	}
	//根据起始点得到世界范围内的随机坐标	
	const Vector3 getWorldRondomPosition(const Vector3 & startPos = Vector3::ZERO);
	//得到世界区域范围
	const Rectangle & getWorldRegionRect(void)	{	return m_region_rect;	}


	const uint32 & getRegionBlockSize(void)		{	return  m_region_block_size;	}
protected:
	uint32		m_default_gid;
	String		m_default_group;
       /// @copydoc ResourceManager::createImpl
        Resource* createImpl(const String& name, ResourceHandle handle,
            const String& group, bool isManual, ManualResourceLoader* loader,
            const NameValuePairList* createParams);

	String getRegionStringName(const uint32 & serial);
	//自动检查启动
	volatile bool	m_autoRegionCheck;
	uint32		m_server_id;
	//每个区域块的大小
	uint32		m_region_block_size;
	//服务器管理的矩形区域大小
	Rectangle	m_world_rect;
	//服务器区域的大小
	Rectangle	m_region_rect;
	//服务器管理区域起始位置
	Vector3		m_world_start_position;


	//记录当前系统毫秒时间
	uint32		m_currentUpdateTime;

	//用于Hash 更新区域	
	std::list<ResourcePtr>		m_RegionQueues;

        //the auto process channel queue pool
        CQueueThreadPool<Resource>     m_RegionProcessPool;
};

//自动处理Region自动信息，将需要处理的Region放入请求队列中.
class RegionThread : public ThreadBase
{       
        RegionManager  * rm;
public: 
        RegionThread(RegionManager * s) :ThreadBase("RegionThread"),rm(s) {}
        bool run()
        {
		Log.Success("RegionThread","Region Process thread start.");       
                return rm->StartRegionProcess();
        }
};

#define   sRegionMgr   RegionManager::getSingleton()


#endif
